module Aggregator
  
  module TagModel
    
    def self.included(base)
      base.class_eval do
        validates_uniqueness_of :name
        validates_presence_of   :name
      end
      base.extend(ClassMethods)
    end
    
    module ClassMethods
    
      ##########################################################
      # Use to generate a tag cloud
      # This method expects that tags contain a 'id', 'name' and a 'count' value
      def cloud(tags, category_list)
        
        if tags and tags.length > 0
          max = 0
          min = tags[0].count.to_i
          tags.each do |tag|
            count = tag.count.to_i
            max = count if count > max
            min = count if count < min
          end
          
          divisor = ((max - min) / category_list.size) + 1
          
          tags.each do |tag|
            yield tag.id, tag.name, category_list[(tag.count.to_i - min) / divisor]             
          end
        end
      end
      
    end
    
  end
end
